/** 主控制器 */
Ext.define("core.app.controller.MainController", {
	extend : "Ext.app.Controller",
	mixins : {
		suppleUtil : "core.util.SuppleUtil",
		messageUtil : "core.util.MessageUtil"
	},
	init : function() {
		var self = this;
		/** 显示登陆窗口 */
		// var loginWin
		// =Ext.create("core.app.view.LoginWindow");
		// loginWin.show();
		/** 公用添加页面的方法 */
		/**
		 * 动态加载controller并渲染它的主窗体
		 */
		this.addFunItem = function(funInfo) {
			if (funInfo) {
				var myMask = new Ext.LoadMask(Ext.ComponentQuery
								.query("panel[xtype=centerview]")[0], {
							msg : "加载页面 中，请您稍等..."
						});
				var mainView = funInfo.mainView;
				var funPanel = mainView.down(funInfo.funViewXtype);
				if (!funPanel) {
					if (null != myMask)
						myMask.show();
					try {
						self.application.getController(funInfo.funController)
								.init();

						funPanel = Ext.create(funInfo.funViewName, {
									closable : true
								});
					} catch (e) {
						myMask.hide();
						//console.log(e);

					}
					mainView.add(funPanel);
					mainView.setActiveTab(funPanel);
					if (null != myMask)
						myMask.hide();
				} else {
					mainView.setActiveTab(funPanel);
				}
			}
		},
		/** 下在是控制部分 */
		this.control({

			
			// 修改密码
			"panel[xtype=mainviewlayout] button[ref=changepassword]" : {
				click : function(btn) {
					Ext.create('Ext.window.Window', {
						title : '修改密码',
						width : 290,
						height : 190,
						layout : 'fit',
						buttonAlign : 'center',
						modal : true,
						items : [{
							xtype : 'form',
							margin : '5 5 5 5',
							defaults : {
								labelSeparator : ':',
								// margin:'10
								// 5 5
								// 5',
								// labelWidth:60,
								// width:400,
								allowBlank : false,
								msgTarget : 'side',
								labelAlign : 'left'
							},
							anchor : '100%',
							// bodyStyle
							// :
							// "background:#DFE9F6",
							border : 0,
							items : [{
										xtype : 'textfield',
										fieldLabel : '原密码',
										name : 'oldPassword',
										inputType : 'password',
										emptyText : '默认密码为工号'

									}, {
										xtype : 'textfield',
										fieldLabel : '新密码',
										name : 'newPassword',
										inputType : 'password'
										
									}, {
										xtype : 'textfield',
										fieldLabel : '确认新密码',
										inputType : 'password',
										validator : function(value) {
											var pw = this.previousSibling().value;
											if (value != pw) {
												return '两次输入的密码不一致';
											} else {
												return true;
											}
										}
									}]
						}],                              
						buttons : [{
							text : '修改',
							handler : function(btn) {
								var win = btn.ownerCt.ownerCt;
								var formObj = win.child('form').getForm();

								if (formObj.isValid()) {
									var oldpwd = formObj
											.findField('oldPassword')
											.getValue();
									var newpwd = formObj
											.findField('newPassword')
											.getValue();
									var resObj = self.ajax({
										url : "dailymanage/personalcenter!changePassword.action",
										params : {
											oldPassword : oldpwd,
											newPassword : newpwd
										}
									});
									if (resObj.success) {
										self.msgbox(resObj.obj);

										btn.ownerCt.ownerCt.close();
										return false;
									} else {
										Ext.Msg.alert("友情提示", resObj.obj);
										return false;
									}

								}
							}
						}, {
							text : '关闭',
							handler : function(btn, o) {
								btn.ownerCt.ownerCt.close();
								return false;
							}
						}]
					}).show();
					return false;
				}
			},
			"panel[xtype=mainviewlayout] button[ref=manual]" : {
				click : function(btn) {


					window.location.href = "login!downloadManual.action";

					return false;
				
				}
			},
			"panel[xtype=mainviewlayout] button[ref=simplemanual]" : {
				click : function(btn) {


					window.location.href = "login!downloadSimpleManual.action";

					return false;
				
				}
			},
			// 退出系统
			"panel[xtype=mainviewlayout] button[ref=logoutsystem]" : {
				click : function(btn) {
					Ext.Msg.confirm('友情提示', '确定要退出系统吗?', function(btn, text) {
								if (btn == 'yes') {
									var resObj = self.ajax({
												url : "login!Logout.action"

											});
									if (resObj.success) {
										location = "login!Login.action";
										return false;
									}
								}
							});
				}
			},

			"westview treepanel" : {
				itemclick : function(tree, record, item, index, e, eOpts) {
					var mainView = tree.up("mainviewlayout").down("centerview");
					//console.log(record.data);
								
					switch (record.data.text) {
						// 部门基本信息管理
						case "jigoumanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "jigoulayout",
								funController : "core.jigou.controller.JiGouController",
								funViewName : "core.jigou.view.JiGouLayout"
							});
							break;
						// 人事基本信息管理
						case "renshimanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "renshilayout",
								funController : "core.renshi.controller.RenShiController",
								funViewName : "core.renshi.view.RenShiLayout"
							});
							break;
						// 岗位信息管理
						case "renshifuzhumanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "renshifuzulayout",
								funController : "core.renshifuzu.controller.RenShiFuZuController",
								funViewName : "core.renshifuzu.view.RenShiFuZuLayout"
							});
							break;
						// 区域信息管理
						case "区域信息管理" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "houselayout",
								funController : "core.house.controller.HouseController",
								funViewName : "core.house.view.HouseLayout"
							});
							break;
						// 应用群组信息管理
						case "appsetmanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "appsetmainlayout",
								funController : "core.appset.controller.AppSetController",
								funViewName : "core.appset.view.MainLayout"
							});
							break;
						// 控制器管理
						case "门禁控制器信息管理" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "doorctrlmainlayout",
								funController : "core.doorctrl.controller.DoorCtrlController",
								funViewName : "core.doorctrl.view.MainLayout"
							});
							break;
						// 房门信息管理
						case "房间信息管理" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "roomlayout",
								funController : "core.room.controller.RoomController",
								funViewName : "core.room.view.RoomLayout"
							});
							break;
						// 门禁卡管理
						case "cardmanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "usercardmainlayout",
								funController : "core.usercard.controller.UserCardController",
								funViewName : "core.usercard.view.MainLayout"
							});
							break;
						// 角色管理
						case "rolemanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "rolelayout",
								funController : "core.role.controller.RoleController",
								funViewName : "core.role.view.RoleLayout"
							});
							break;
						// 远程控制
						case "remotecontrol" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "remotelayout",
								funController : "core.remotecontrol.controller.RemoteController",
								funViewName : "core.remotecontrol.view.RemoteLayout"
							});
							break;
						// 个人中心
						case "personalinfo" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "percentermainlayout",
								funController : "core.percenter.controller.PersonalCenterController",
								funViewName : "core.percenter.view.MainLayout"
							});
							break;
						// 门禁权限管理
						case "copyshouquan" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "copyshouquanmainlayout",
								funController : "core.copyshouquan.controller.CopyRightController",
								funViewName : "core.copyshouquan.view.MainLayout"
							});
							break;
						 //门禁实时监控
//						case "monitormanage" :
//							self.addFunItem({
//								mainView : mainView,
//								funViewXtype : "monitormainlayout",
//								funController : "core.monitor.controller.MonitorController",
//								funViewName : "core.monitor.view.MainLayout"
//							});
//							break;
						// 发布新消息
						case "publishmessage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "publishmessagepanel",
								funController : "core.msg.controller.PublishMessageController",
								funViewName : 'core.msg.view.PublishMessagePanel'
							});
							break;
						// 人员进出记录报表
						case "inoutreport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "reportinoutgrid",
								funController : "core.report.inout.controller.InOutController",
								funViewName : "core.report.inout.view.InOutDataGrid"
							});
							break;
						// 消息管理
						case "messagemanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "allmessagegrid",
								funController : "core.msgmanage.controller.AllMessageController",
								funViewName : "core.msgmanage.view.AllMessageGrid"
							});
							break;
						// 人员增减报表
						case "peopleadddeletereport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "peoplezjgrid",
								funController : "core.report.peoplezj.controller.PeopleZjController",
								funViewName : "core.report.peoplezj.view.PeopleZjGrid"
							});
							break;
						// 门禁权限下载通讯报表
						case "rightdownloadreport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "shouquandownloadgrid",
								funController : "core.report.quanxiandownloadinfo.controller.ShouQuanDownloadController",
								funViewName : "core.report.quanxiandownloadinfo.view.ShouQuanDownloadGrid"
							});
							break;
						// 岗位权限报表
						case "gwrightreport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "gwrightgrid",
								funController : "core.report.gwright.controller.GwRightController",
								funViewName : "core.report.gwright.view.GwRightGrid"
							});
							break;
						case "remotereport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "remoteloggrid",
								funController : "core.report.remotelog.controller.RemoteLogController",
								funViewName : "core.report.remotelog.view.RemoteLogGrid"
							});
							break;
						case "stationlogreport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "commandloggrid",
								funController : "core.report.commandlog.controller.CommandLogController",
								funViewName : "core.report.commandlog.view.CommandLogGrid"
							});
							break;
						case "servercommandreport" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "servercommandgrid",
								funController : "core.report.servercommand.controller.ServerCommandController",
								funViewName : "core.report.servercommand.view.ServerCommandGrid"
							});
							break;
						case "clearsystemdata" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "cleardatapanel",
								funController : "core.system.cleardata.controller.ClearDataController",
								funViewName : "core.system.cleardata.view.ClearDataPanel"
							});
							break;
						case "databasemanage" :
							self.addFunItem({
								mainView : mainView,
								funViewXtype : "dbgrid",
								funController : "core.system.dbbackup.controller.DbBackupController",
								funViewName : "core.system.dbbackup.view.DbGrid"
							});
							break;
					}
				}
			}
		});
	},
	views : ["core.app.view.WestView",
			"core.app.view.CenterView", "core.app.view.MainViewLayout"
			],
	stores : [],
	models : []
});